/*
 * Initialize JSON arrays for Client Information tabs
 */
var clientInfoSocialJSONArray       = [];
var clientInfoSchoolTransJSONArray  = [];
var clientInfoFamilyJSONArray       = [];
var clientInfoEventsJSONArray       = [];
var clientInfoOtherJSONArray        = [];
var clientInfoLibraryJSONArray      = [];
var clientInfoPartnersJSONArray     = [];
var clientInfoSubscriptionJSONArray = [];

var contactFormGetInfoObj = {
	getMemberInformation : function(familyID,memberID,tabID) {
		var contactID = $F('contactID');
		var newFamilyID = contactFormSupportObj.setActiveFamilyID(familyID,'member');
		var params = 'handler=getMemberInformation&memberID='+memberID+'&tabID='+tabID+'&newFamilyID='+newFamilyID+'&contactID='+contactID+'&assign=1';
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				contactFormGetInfoObj.returnGetMemberInformation(transport.responseText);
			},
			onFailure: function() {
				alert('Oops!');
			}	
		});
	},
	getFamilyInformation : function(familyID,tabID) {
		var contactID = $F('contactID');
		var newFamilyID = contactFormSupportObj.setActiveFamilyID(familyID,'family');
		var params = 'handler=getFamilyInformation&familyID='+familyID+'&tabID='+tabID+'&newFamilyID='+newFamilyID+'&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				contactFormGetInfoObj.returnGetFamilyInformation(transport.responseText);
			},
			onFailure: function() {
				alert('Oops!');
			}	
		});
	},
	returnGetMemberInformation : function(memberJSONString,tabID) {
		/*
		 * Parse JSON string
		 * Don't eval memberJSONString unless we know it's a string 
		 */
		if (typeof(memberJSONString) != 'object') {
			var jsonObject = eval('(' + memberJSONString + ')');
		} else {
			var jsonObject = memberJSONString;
		}	
		var memberID     = jsonObject.MemberID;
		var nameFirst    = jsonObject.FirstName;  
		var nameLast     = jsonObject.LastName;
		var address      = jsonObject.Address;
		var city         = jsonObject.City;
		var state        = jsonObject.State;
		var zip          = jsonObject.Zip;
		var county       = jsonObject.County;
		var email        = jsonObject.Email1;
		var email2       = jsonObject.Email2;
		var phone1       = jsonObject.Phone1;
		var phone2       = jsonObject.Phone2;
		var phone3       = jsonObject.Phone3;
		var phone4       = jsonObject.Phone4;		
		var gender       = jsonObject.Gender;
		var memberType   = jsonObject.MemberType;
		var singleParent = jsonObject.SingleParent;
		var roleIDs      = jsonObject.RoleIDs;
		var childDOB     = jsonObject.ChildDOB;
		var childAge     = jsonObject.ChildAge;
		var ethnicity    = jsonObject.Ethnicity;
		var language     = jsonObject.PrimaryLanguage;
		var countyFIPS   = jsonObject.CountyFIPS;
		var orgName      = jsonObject.OrganizationName;
		var orgTitle     = jsonObject.OrganizationTitle;
		var orgURL       = jsonObject.OrganizationURL;
		var schoolType   = jsonObject.TypeOfSchool;
		var schoolName   = jsonObject.SchoolName;
		var schoolDistrict = jsonObject.SchoolDistrict;
		var mayContact     = jsonObject.MayContact;		
		var mayContactPhoneHome = jsonObject.MayContactPhoneHome;
		var mayContactPhoneWork = jsonObject.MayContactPhoneWork;
		var mayContactEmail     = jsonObject.MayContactEmail;
		var lowLit              = jsonObject.LowLit;
		var lowIncome           = jsonObject.LowIncome;
		
		if (!tabID) {
			var tabID  = jsonObject.TabID;		
		}	
				
		/*
		 * Populate tab:tabID with member info.
		 */
		$('memberID' + tabID).value   = memberID;
		$('nameFirst' + tabID).value  = nameFirst;
		$('nameLast' + tabID).value   = nameLast;
		$('zipCode' + tabID).value    = zip;
		$('address' + tabID).value    = address;
		$('city' + tabID).value       = city;
		$('phone1' + tabID).value     = phone1;
		$('phone2' + tabID).value     = phone2;
		$('phone3' + tabID).value     = phone3;
		$('phone4' + tabID).value     = phone4;
		$('email1' + tabID).value     = email;
		$('email2' + tabID).value     = email2;
		$('dateFieldBirth'+tabID).value      = childDOB;
		$('nameOfSchool' + tabID).value      = schoolName;
		$('professionalOrg' + tabID).value   = orgName;
		$('professionalTitle' + tabID).value = orgTitle;
		$('professionalURL' + tabID).value   = orgURL;
		
		/*
		 * State select
		 */
		var optionsState = $('state'+tabID).options;
		for(a=0;a<optionsState.length;a++) {
			if ($('state'+tabID).options[a].value == state) {
				$('state'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * Language select
		 */
		var optionsLanguage = $('selectLanguage'+tabID).options;
		for(a=0;a<optionsLanguage.length;a++) {
			if ($('selectLanguage'+tabID).options[a].value == language) {
				$('selectLanguage'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * County select
		 */
		var optionsCounty = $('selectCounty'+tabID).options;
		for(a=0;a<optionsCounty.length;a++) {
			if ($('selectCounty'+tabID).options[a].value == county) {
				$('selectCounty'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * Ethnicity select
		 */
		var optionsEthnicity = $('selectEthnicity'+tabID).options;
		for(a=0;a<optionsEthnicity.length;a++) {
			if ($('selectEthnicity'+tabID).options[a].value == ethnicity) {
				$('selectEthnicity'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * School District select
		 */
		$('selectSchoolDistrict'+tabID).value = schoolDistrict;
		
		/*
		 * Parse role string
		 */
		var roleArray  = roleIDs.split('|');
		var roleParent       = roleArray[0];
		var roleProfessional = roleArray[1];
		var roleOther        = roleArray[2];
				
		/*
		 * Parent Role select
		 */
		var optionsRoleParent = $('parentRole'+tabID).options;
		for(a=0;a<optionsRoleParent.length;a++) {
			if ($('parentRole'+tabID).options[a].value == roleParent) {
				$('parentRole'+tabID).options[a].selected = true;
			}
		}		
		
		/*
		 * Single parent
		 */
		if (singleParent == 1) {
			$('parentSingle'+tabID).checked = true;
		}		
			
		/*
		 * Professional Role select
		 */
		var optionsRoleProfessional = $('professionalRole'+tabID).options;
		for(a=0;a<optionsRoleProfessional.length;a++) {
			if ($('professionalRole'+tabID).options[a].value == roleProfessional) {
				$('professionalRole'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * Other Role select
		 */
		var optionsRoleOther = $('otherRole'+tabID).options;
		for(a=0;a<optionsRoleOther.length;a++) {
			if ($('otherRole'+tabID).options[a].value == roleOther) {
				$('otherRole'+tabID).options[a].selected = true;
			}
		}
		
		/*
		 * Parse childAge
		 */
		var childAgeArray = childAge.split('|');
		
		var optionsAgeYear = $('childAgeYr'+tabID).options;
		for(a=0;a<optionsAgeYear.length;a++) {
			if ($('childAgeYr'+tabID).options[a].value == childAgeArray[0]) {
				$('childAgeYr'+tabID).options[a].selected = true;
			}
		}
		
		var optionsAgeMonth = $('childAgeMonth'+tabID).options;
		for(a=0;a<optionsAgeMonth.length;a++) {
			if ($('childAgeMonth'+tabID).options[a].value == childAgeArray[1]) {
				$('childAgeMonth'+tabID).options[a].selected = true;
			}
		}
		
		if (gender == 'Male') {
			$('memberGenderMale'+tabID).checked = true;
		} else if (gender == 'Female') {
			$('memberGenderFemale'+tabID).checked = true;
		}
		
		/*
		 * Parse member type string
		 */
		var memberTypeArray = memberType.split('|');
		
		if (memberTypeArray[0] == 'Child') {
			$('memberTypeChild'+tabID).checked = true;
		}
		if (memberTypeArray[1] == 'Parent') {
			$('memberTypeParent'+tabID).checked = true;
		}
		if (memberTypeArray[2] == 'Professional') {
			$('memberTypeProfessional'+tabID).checked = true;
		}
		if (memberTypeArray[3] == 'Other') {
			$('memberTypeOther'+tabID).checked = true;
		}		 
		
		/*
		 * School type
		 */
		if (schoolType == 'Public') {
			$('schoolTypePublic'+tabID).checked = true;
		} else if (schoolType == 'Private') {
			$('schoolTypePrivate'+tabID).checked = true;
		} else if (schoolType == 'Home') {
			$('schoolTypeHome'+tabID).checked = true;
		} else if (schoolType == 'Charter') {
			$('schoolTypeCharter'+tabID).checked = true;
		}
		
		/*
		 * OK to contact by...
		 */
		if (mayContactPhoneHome == 1) {
			$('okToContactPhoneHome'+tabID).checked = true;
		}
		if (mayContactPhoneWork == 1) {
			$('okToContactPhoneWork'+tabID).checked = true;
		}
		if (mayContactEmail == 1) {
			$('okToContactEmail'+tabID).checked = true;
		}
		
		/*
		 * Low literacy and low income
		 */
		if (lowLit == 1) {
			$('familyLowLiteracy'+tabID).checked = true;
		}
		if (lowIncome == 1) {
			$('familyLowIncome'+tabID).checked = true;
		}
		
		/*
		 * Set tab title to member full name
		 */
		var tabTitle = nameFirst+' '+nameLast;
		tabTitle = tabTitle.trim();
		contactFormSupportObj.updateTabTitle(tabID,tabTitle);
		
		/*
		 * Turn on buttonDeleteMember
		 */
		$('buttonDeleteMember' + tabID).style.display = '';
		
		/*
		 * Pull Client Information data for memberID
		 */
		this.getClientInfoInit(tabID,memberID);
	},
	returnGetFamilyInformation : function(familyJSONString) {
		var mode = $F('action');
				
		/*
		 * If in edit mode and no members, display blank member panel
		 */		
		if (mode == 'edit') {	
			/*
			 * Close all family tabs
			 */
			//contactFormSupportObj.closeAllFamilyTabs();
		}	
				
		if ( (mode == 'edit') && (familyJSONString == 0) ) {    
			/*
			 * Create one blank member tab if we don't have any members
			 * for this contact
			 */
            contactFormSupportObj.addNewMemberTab();		
		} else {		
			var jsonObject = eval('('+familyJSONString+')');
			var totalMembers = (jsonObject.Members.length)-1;
			
			for(i=0;i<=totalMembers;i++) {
				var memberJSONString = jsonObject.Members[i];	
				var memberID = jsonObject.Members[i].MemberID;	
				var tabID    = jsonObject.Members[i].TabID;	
				/*
				 * Our first member JSON string will have the initial tabID
				 */	
				if (i > 0) {
					/*
					 * Create new tab
					 */
					contactFormSupportObj.addNewMemberTab(memberID);
				} else {
					if (mode == 'add') {
						/*
						 * This tab is already active. Populate its form fields with JSON data.
						 */					
						contactFormGetInfoObj.returnGetMemberInformation(memberJSONString,1);
					} else if (mode == 'edit') {
						/*
						 * We need to add a first tab if we're in edit mode
						 */					
						contactFormSupportObj.addNewMemberTab(memberID);
					}
				}			
			}	
		}	
	},	
	getClientInfoSocial : function(memberID) {
		var params = 'handler=getClientInfoSocial&memberID='+memberID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					clientInfoSocialJSONArray[memberID] = transport.responseText;
					contactFormGetInfoObj.populateClientInfoSocial(clientInfoSocialJSONArray[memberID]);
				}	
			}	
		});
	},
	getClientInfoSchoolTrans : function(memberID) {
		var params = 'handler=getClientInfoSchoolTrans&memberID='+memberID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					clientInfoSchoolTransJSONArray[memberID] = transport.responseText;
					contactFormGetInfoObj.populateClientInfoSchoolTrans(clientInfoSchoolTransJSONArray[memberID]);
				}	
			}	
		});
	},
	getClientInfoFamily : function(memberID) {
		var params = 'handler=getClientInfoFamily&memberID='+memberID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					clientInfoFamilyJSONArray[memberID] = transport.responseText;
					contactFormGetInfoObj.populateClientInfoFamily(clientInfoFamilyJSONArray[memberID]);
				}	
			}	
		});
	},
	getClientInfoEvents : function(memberID) {
		var params = 'handler=getClientInfoEvents&memberID='+memberID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					clientInfoEventsJSONArray[memberID] = transport.responseText;
					contactFormGetInfoObj.populateClientInfoEvents(clientInfoEventsJSONArray[memberID]);
				}	
			}	
		});
	},
	getClientInfoLibrary : function(memberID) {
		// TODO: Get client library data
	},
	getClientInfoPartners : function(memberID) {
		// TODO: Get client partner data
	},
	getClientInfoSubscription : function(memberID) {
		// TODO: Get client subscriptions
	},
	getClientInfoInit : function (tabID,memberID) {		
		/*
		 * Clear Client Information panels
		 */
		contactFormSupportObj.clearClientInfoTabs();
		
		/*
		 * Try to get memberID
		 */
		if (!memberID) {
			if ($('memberID'+tabID)) {
				memberID = $F('memberID'+tabID);
			}
		}
		if (memberID) {
			/*
			 * Set activeMemberID
			 */
			contactFormSupportObj.setActiveMemberID(memberID);
			
			/*
			 * Get member name and update Client Information heading
			 */
			var memberFullName = $F('nameFirst'+tabID)+' '+$F('nameLast'+tabID);
			$('clientName').innerHTML = '('+memberFullName+')';
			
			var clientInfoSocialJSON       = clientInfoSocialJSONArray[memberID];
			var clientInfoSchoolTransJSON  = clientInfoSchoolTransJSONArray[memberID];
			var clientInfoFamilyJSON       = clientInfoFamilyJSONArray[memberID];
			var clientInfoEventsJSON       = clientInfoEventsJSONArray[memberID];
			var clientInfoLibraryJSON      = clientInfoLibraryJSONArray[memberID];
			var clientInfoPartnersJSON     = clientInfoPartnersJSONArray[memberID];
			var clientInfoSubscriptionJSON = clientInfoSubscriptionJSONArray[memberID];
						
			if (!clientInfoSocialJSON) {
				this.getClientInfoSocial(memberID);
			} else {
				this.populateClientInfoSocial(clientInfoSocialJSON);
			}
			if (!clientInfoSchoolTransJSON) {
				this.getClientInfoSchoolTrans(memberID);
			} else {
				this.populateClientInfoSchoolTrans(clientInfoSchoolTransJSON);
			}
			if (!clientInfoFamilyJSON) {
				this.getClientInfoFamily(memberID);
			} else {
				this.populateClientInfoFamily(clientInfoFamilyJSON);
			}
			if (!clientInfoEventsJSON) {
				this.getClientInfoEvents(memberID);
			} else {
				this.populateClientInfoEvents(clientInfoEventsJSON);
			}
			if (!clientInfoLibraryJSON) {
				this.getClientInfoLibrary(memberID);
			} else {
				this.populateClientInfoLibrary(clientInfoLibraryJSON);
			}
			if (!clientInfoPartnersJSON) {
				this.getClientInfoPartners(memberID);
			} else {
				this.populateClientInfoPartners(clientInfoPartnersJSON);
			}
			if (!clientInfoSubscriptionJSON) {
				this.getClientInfoSubscription(memberID);
			} else {
				this.populateClientInfoSubscription(clientInfoSubscriptionJSON);
			}
		}
	},	
	populateClientInfoSocial : function (clientInfoJSONString) {
		if (typeof(clientInfoJSONString) != 'object') {
			var jsonObject = eval('(' + clientInfoJSONString + ')');
		} else {
			var jsonObject = clientInfoJSONString;
		}	
		
		var entryID     = jsonObject.EntryID;
		var memberID    = jsonObject.MemberID;
		var concerns    = jsonObject.Concerns;  
		var response    = jsonObject.Response;
		
		if (typeof(concerns) == 'undefined') {
			concerns = '';
		}
		if (typeof(response) == 'undefined') {
			response = '';
		}		
		
		$('socialConcerns').value = concerns;
		$('socialResponse').value = response;
	},
	populateClientInfoSchoolTrans : function (clientInfoJSONString) {
		if (typeof(clientInfoJSONString) != 'object') {
			var jsonObject = eval('(' + clientInfoJSONString + ')');
		} else {
			var jsonObject = clientInfoJSONString;
		}	
		
		var entryID          = jsonObject.EntryID;
		var memberID         = jsonObject.MemberID;
		var iepClass1        = jsonObject.IEPClass1;
		var iepClass2        = jsonObject.IEPClass2;
		var spedDesignation  = jsonObject.SPEDDesignation;
		var diplomaType      = jsonObject.DiplomaType;
		var concerns         = jsonObject.Concerns;  
		var response         = jsonObject.Response;
				
		/*
		 * IEP Classification 1
		 */
		var optionsIEPClass1 = $('schoolIEPClassification1').options;
		for(a=0;a<optionsIEPClass1.length;a++) {
			if ($('schoolIEPClassification1').options[a].value == iepClass1) {
				$('schoolIEPClassification1').options[a].selected = true;
			}
		}
		
		/*
		 * IEP Classification 2
		 */
		var optionsIEPClass2 = $('schoolIEPClassification2').options;
		for(a=0;a<optionsIEPClass2.length;a++) {
			if ($('schoolIEPClassification2').options[a].value == iepClass2) {
				$('schoolIEPClassification2').options[a].selected = true;
			}
		}
		
		/*
		 * SPED Designation
		 */
		var optionsSPED = $('schoolSPEDDesignation').options;
		for(a=0;a<optionsSPED.length;a++) {
			if ($('schoolSPEDDesignation').options[a].value == spedDesignation) {
				$('schoolSPEDDesignation').options[a].selected = true;
			}
		}
		
		/*
		 * Diploma Type
		 */
		var optionsDiplomaType = $('schoolDiplomaType').options;
		for(a=0;a<optionsDiplomaType.length;a++) {
			if ($('schoolDiplomaType').options[a].value == diplomaType) {
				$('schoolDiplomaType').options[a].selected = true;
			}
		}
		
		if (typeof(concerns) == 'undefined') {
			concerns = '';
		}
		if (typeof(response) == 'undefined') {
			response = '';
		}		
		
		$('schoolConcerns').value = concerns;
		$('schoolResponse').value = response;
	},
	populateClientInfoFamily : function (clientInfoJSONString) {
		if (typeof(clientInfoJSONString) != 'object') {
			var jsonObject = eval('(' + clientInfoJSONString + ')');
		} else {
			var jsonObject = clientInfoJSONString;
		}	
		
		var entryID     = jsonObject.EntryID;
		var memberID    = jsonObject.MemberID;
		var concerns    = jsonObject.Concerns;  
		var response    = jsonObject.Response;
		
		if (typeof(concerns) == 'undefined') {
			concerns = '';
		}
		if (typeof(response) == 'undefined') {
			response = '';
		}		
		
		$('familyConcerns').value = concerns;
		$('familyResponse').value = response;
	},
	populateClientInfoEvents : function (clientInfoJSONString) {
		var jsonObject  = clientInfoJSONString.evalJSON();
		var totalEvents = jsonObject.MemberEvents.length;
		var stringOut   = '';
		
		if (totalEvents>=0) {	
			stringOut += '<table class="dataTable">';
			stringOut += '<tr>';
			stringOut += '<th style="width: 75px;">Type</th>';
			stringOut += '<th>Event</th>';
			stringOut += '<th style="width: 150px;">Date(s)</th>';
			stringOut += '<th style="text-align: center; width: 75px;">Registered</th>';
			stringOut += '<th style="text-align: center; width: 75px;">Attended</th>';
			stringOut += '</tr>';
    		
			for(a=0;a<=(totalEvents-1);a++) {
				var eventCat  = jsonObject.MemberEvents[a].CatTitle;
				var eventName = jsonObject.MemberEvents[a].EventName;
				var regID     = jsonObject.MemberEvents[a].RegistrationID;
				var attended  = jsonObject.MemberEvents[a].Attended;
				var eventID   = jsonObject.MemberEvents[a].EventID;
				var startDate = jsonObject.MemberEvents[a].StartDate;
				var endDate   = jsonObject.MemberEvents[a].EndDate;
				var dateReg   = jsonObject.MemberEvents[a].DateRegister;
				var eventDesc = jsonObject.MemberEvents[a].EventDescription;
				
				if (attended == 1) {
					var attendIcon = '<div id="attendIcon'+regID+'" onClick="contactSaveClientInfoObj.toggleClientInfoEventAttended('+regID+'); return false;" style="cursor: pointer;" ext:qtip="Attended event." class="statusClosed"></div>';
				} else {
					var attendIcon = '<div id="attendIcon'+regID+'" onClick="contactSaveClientInfoObj.toggleClientInfoEventAttended('+regID+'); return false;" style="cursor: pointer;" ext:qtip="Has not attended event. Click to change status." class="statusAlert"></div>';
				}
				
				stringOut += '<tr>';
				stringOut += '<td>'+eventCat+'</td>';
				stringOut += '<td><a href="#" onClick="calendarUpdateObj.getEventData('+eventID+',\'view\'); return false;" ext:qtip="'+eventDesc+'">'+eventName+'</a></td>';
				stringOut += '<td>'+startDate;
				
				if (endDate != '') {
					stringOut += ' to '+endDate;
				}
				
				stringOut += '</td>';
				stringOut += '<td><div ext:qtip="Registered for event on '+dateReg+'" class="statusClosed"></div></td>';
				stringOut += '<td>'+attendIcon+'</td>';
				stringOut += '</tr>';
			}
			stringOut += '</table>';
		}
		$('memberEvents').innerHTML = stringOut;
	},
	populateClientInfoLibrary : function (clientInfoJSONString) {
		// TODO: Populate library tab
	},
	populateClientInfoPartners : function (clientInfoJSONString) {
		// TODO: Populate partners tab
	},
	populateClientInfoSubscription : function (clientInfoJSONString) {
		// TODO: Ppoulate subscriptions tab
	},
	getContactFormInfoForEdit : function (contactID) {
		/*
		 * Get general contact form information
		 */
		var params = 'handler=getContactGeneralInformation&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					contactFormGetInfoObj.returnGetContactGeneralInformation(transport.responseText);
				}	
			}	
		});
		
		/*
		 * Get members for this contact
		 */
		var params = 'handler=getContactMembersInformation&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport){
				if (transport.responseText != 'false') {
					contactFormGetInfoObj.returnGetFamilyInformation(transport.responseText);
				}	
			}	
		});			
	},
	returnGetContactGeneralInformation : function (JSONString) {
		if (typeof(JSONString) != 'object') {
			var jsonObject = eval('(' + JSONString + ')');
		} else {
			var jsonObject = JSONString;
		}
		
		$('contactIDNo').innerHTML   = jsonObject.IDNumber;
		$('contactSubject').value    = jsonObject.SubjectLine;
		$('dateOfContact').innerHTML = jsonObject.DateofContact;
		
		/*
		 * Set entered by name
		 */
		$('staffMember').innerHTML = jsonObject.enteredByStaffName;
		
		/*
		 * Change window tab title from 'New Contact' to contact ID and subject
		 */
		var tabContent = '#'+jsonObject.IDNumber+' '+jsonObject.SubjectLine;
		var pageTabObject = mainTabs.getItem('pageTab0');
		pageTabObject.setTitle(tabContent);	
		
		/*
		 * Set contact status
		 */
		$('contactFormStatusInner').innerHTML = '<span class="grayText">Status:</span> '+jsonObject.statusText;
		$('contactFormStatus').className = jsonObject.statusClassBox;
		
		/*
		 * Populate referral items
		 */
		if (jsonObject.ReferralLibrary == 1) {
			$('LibraryItems').checked = true;
			contactFormGetInfoObj.getLibraryReferralItems();			
		}
		if (jsonObject.ReferralOrFIRST == 1) {
			$('OrFIRSTItems').checked = true;
		}
		if (jsonObject.ReferralCareNotebook == 1) {
			$('CareNotebookItems').checked = true;
			/*
			 * Parse care notebook language
			 */
			var cnLangArray = jsonObject.ReferralCareNotebookLanguage.split('|');
			if (cnLangArray[0] == 'English') {
				$('careNotebookEnglish').checked = true;
			}
			if (cnLangArray[1] == 'Spanish') {
				$('careNotebookSpanish').checked = true;
			}
			if (cnLangArray[2] == 'Russian') {
				$('careNotebookRussian').checked = true;
			}
			
			$('careNotebookNumber').value = jsonObject.ReferralCareNotebookNumber;			
		}
		if (jsonObject.ReferralEvent == 1) {
			$('EventItems').checked = true;
			/*
			 * Render event referral items 
			 */
			var params = 'handler=getEventReferrals&contactID='+jsonObject.ContactID;
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport){
					var eventJSON = eval('(' + transport.responseText + ')'); 
					
					/*
					 * Check appropriate events
					 */
					var totalEvents = eventJSON.EventIDs.length;
					for(a=0;a<totalEvents;a++) {
						eventID = eventJSON.EventIDs[a].EventID;
						$('referralEventID_'+eventID).checked = true;
					}					
				}	
			});				
		}
		if (jsonObject.ReferralOrPTI == 1) {
			$('OrPTIItems').checked = true;
		}
		if (jsonObject.ReferralDocument == 1) {
			$('DocumentItems').checked = true;
			/*
			 * TODO: Render document referral items 
			 */
		}
		if (jsonObject.ReferralFollowUp == 1) {
			$('FollowUpItems').checked = true;
			
			/*
			 * Get follow-up staff and other information
			 */
			var params = 'handler=getFollowUpInformation&contactID='+jsonObject.ContactID;
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport){
					var response = transport.responseText;
					var jsonArray = response.split('|');
					
					var staffJSON = eval('(' + jsonArray[0] + ')'); 
					var followUpJSON = eval('(' + jsonArray[1] + ')');
					
					/*
					 * Check staff
					 */
					var totalStaff = staffJSON.Staff.length;
					for(a=0;a<totalStaff;a++) {
						staffID = staffJSON.Staff[a].StaffID;
						$('staffID_'+staffID).checked = true;
					}
										
					/*
					 * Populate follow-up form
					 */
					var followUpNotes = followUpJSON.Message;
					var followUpRecipients = followUpJSON.AdditionalRecipients;
					
					$('followUpNotes').value = followUpNotes;
					$('followUpAdditionalRecipients').value = followUpRecipients;
					
					$('followUpNotes').style.color = '#000';
					$('followUpAdditionalRecipients').style.color = '#000';
				}	
			});					
		}
		if (jsonObject.ReferralBrochure == 1) {
			$('BrochureItems').checked = true;
			
			$('brochureNumber').value = jsonObject.ReferralBrochureNumber;
			
			var optionsBrochureType = $('brochureType').options;
			for(a=0;a<optionsBrochureType.length;a++) {
				if ($('brochureType').options[a].value == jsonObject.ReferralBrochureType) {
					$('brochureType').options[a].selected = true;
				}
			}
		}
		if (jsonObject.ReferralLink == 1) {
			$('LinkItems').checked = true;
					
			if (jsonObject.ReferralLinkURL != '') {
				$('linkURLs').value = jsonObject.ReferralLinkURL;
				$('linkURLs').style.color = '#000';
			}	
		}		
	},
	getLibraryReferralItems : function() {
		var contactID = $F('contactID');
		
		var params = 'handler=getLibraryItems&contactID='+contactID+'&type=contact';
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport){
					contactFormGetInfoObj.renderLibraryReferralItems(transport.responseText);
				}	
			});				
	},
	renderLibraryReferralItems : function(stringJSON) {
		var stringOut = '';
		if (stringJSON != 0) {
			var jsonObject = stringJSON.evalJSON();
		
			var totalItems = (jsonObject.LibraryItems.length)-1;
		
			if (totalItems>=0) {			
				stringOut += '<table class="dataTable" style="width: 100%;">';
				stringOut += '<tr><th style="width: 30px;"></th><th>Title</th></tr>';
					
				for(a=0;a<=totalItems;a++) {
					var libraryID    = jsonObject.LibraryItems[a].LibraryID;
					var title        = jsonObject.LibraryItems[a].Title;
					var itemAbstract = jsonObject.LibraryItems[a].Abstract;
					
					if ((a % 2) > 0) { 
						rowClass = 'altRow'; 
					} else { 
						rowClass = '';
					}			
					
					stringOut += '<tr class="'+rowClass+'" id="libraryReferralRow'+libraryID+'">';
					stringOut += '<td style="text-align: center;"><div class="iconRemove" style="cursor: pointer;" title="Remove item" onClick="contactFormSupportObj.removeLibraryReferralItem('+libraryID+'); return false;"></div></td>';
					stringOut += '<td><div ext:qtip="'+itemAbstract+'">'+title+'</div></td>';
					stringOut += '</tr>';				
				}		
				
				stringOut += '</table>';
			}		
		}	
		$('referralLibraryItems').innerHTML = stringOut;
	}
} 